
var pilihanNumber = 0;
var id = 0;

(function($) {
	$(document).ready(function(e) {
		
		//detail...................................
		$("#dialog-tambah-soal").dialog({
			autoOpen: false, 
			title: "Tambah Soal", 
			modal: true, 
			width: "650", 
			height: "600"
			
		});
		
		$("li #tambah-soal").on('click', function(event){
			$("#dialog-tambah-soal").dialog("open");
			event.preventDefault();
		});
		
		
		$("#btn-tambah").click(function(){
			pilihanNumber++;
			var pilihanText = $("#pilihan-text");
			pilihanText.html("pilihan "+pilihanNumber);
            pilihanText.removeAttr( "id" );
            var inputTambah = $("#input-tambah-pilihan").html();
            pilihanText.attr( "id", "pilihan-text" );
            var rowForm = $('<div>' + inputTambah + '</div>').find(".row-form");
            rowForm.insertAfter("#last-pilihan");
           
        });  
		
		$('[name="no"]').change(function() {
			var url = "functions/crud/crud_d_soal.php";
			var no = $('[name="no"]').val();
			var soalId = $('[name="soal_id"]').val();
			$.post(url, {"no": no, "soal_id":soalId, "operation": "get_data"} ,function(data) {
				//alert(data);
				if(data!="failed"){
					var obj = eval ("(" + data + ")");
					$('[name="pertanyaan"]').val(obj.soals[0].pertanyaan);
					$('[name="pilihan_a"]').val(obj.soals[0].pilihan_a);
					$('[name="pilihan_b"]').val(obj.soals[0].pilihan_b);
					$('[name="pilihan_c"]').val(obj.soals[0].pilihan_c);
					$('[name="pilihan_d"]').val(obj.soals[0].pilihan_d);
					$('[name="jawaban"]').val(obj.soals[0].jawaban);
					$('#id').val(obj.soals[0].id);
					var text = "";
					if(obj.soals[0].jawaban == "1"){
						text = "Pilihan a";
					}else if(obj.soals[0].jawaban == "2"){
						text = "Pilihan b";
					}else if(obj.soals[0].jawaban == "3"){
						text = "Pilihan c";
					}else if(obj.soals[0].jawaban == "4"){
						text = "Pilihan d";
					}
					$('#s2id_autogen2 .select2-choice span').text(text);
					$('#operation').val('edit');
				}else{
					$('[name="pertanyaan"]').val('');
					$('[name="pilihan_a"]').val('');
					$('[name="pilihan_b"]').val('');
					$('[name="pilihan_c"]').val('');
					$('[name="pilihan_d"]').val('');
					$('[name="jawaban"]').val('1');
					$('#id').val('');
					$('#s2id_autogen2 .select2-choice span').text('Pilihan a');					
					$('#operation').val('add');
				}
				
				
			});
		});
	});
}) (jQuery);
